<?php
class Api_Calls_Thread extends Api_Calls
{
	public static function getThread($threadid)
	{
		$thread = self::getThreadInfo($threadid);
		
		
			$bbCodeParser = new XenForo_BbCode_Parser(new XenForo_BbCode_Formatter_Wysiwyg);
			$bbCodeOptions = array(
				'showSignature' => false,
				'states' => array( 
					'viewAttachments' => false
				)
			);
			/** @var $postModel XenForo_Model_Post */
			$postModel = self::getModelFromCache('XenForo_Model_Post');	
			$posts = $postModel->getPostsInThread($thread['thread_id']);
			$postarray = array();
			foreach ($posts as $postid	=>	$post) {
				
				$postarray[$postid] = 	array(
					'parentid'	=>	$post['post_id'] == $thread['first_post_id'] ? '0' : $thread['first_post_id'],				
					'userid'	=>	$post['user_id'],
					'post_date'	=>	$post['post_date'],
					'message'	=>	XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($post, $bbCodeParser, $bbCodeOptions)->__toString()
				);
			}
			
			$thread['posts'] = $postarray;
			
			return $thread;
	}
	
	/**
	 * 
	 * returns all informations of a thread
	 * @param int $threadid
	 */
	public static function getThreadInfo($threadid)
	{
		$threadModel = self::getModelFromCache('XenForo_Model_Thread');
		$thread = $threadModel->getThreadById($threadid);
		return $thread;
	}
}